package com.morehair.service.wx.impl;

import com.morehair.bean.pojo.Address;
import com.morehair.bean.pojo.AddressExample;
import com.morehair.bean.pojo.Region;
import com.morehair.bean.pojo.User;
import com.morehair.bean.vo.wx.WxAddressVo;
import com.morehair.mapper.AddressMapper;
import com.morehair.mapper.RegionMapper;
import com.morehair.service.wx.WxAddressService;
import com.morehair.utils.GetNowUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.LongStream;

/**
 * Author  kkw
 * Date  2021/6/8 19:25
 * Version 1.0
 * Description :
 */
@Service
@Transactional
public class WxAddressServiceImpl implements WxAddressService {
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;

    @Override
    public List<WxAddressVo> getAddress() {
        Integer id = GetNowUser.getUser().getId();
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andUserIdEqualTo(id).andDeletedEqualTo(false);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        ArrayList<WxAddressVo> wxAddressVos = new ArrayList<>();
        for (Address address : addresses) {
            WxAddressVo addressVo = new WxAddressVo();
            addressVo.setIsDefault(address.getIsDefault());
            addressVo.setName(address.getName());
            addressVo.setMobile(address.getMobile());
            addressVo.setId(address.getId());

            String province=getRegionName(address.getProvinceId());
            String city=getRegionName(address.getCityId());
            String area=getRegionName(address.getAreaId());
            String detailAddress=province+city+area;
            addressVo.setDetailedAddress(detailAddress);
            wxAddressVos.add(addressVo);
        }

        return wxAddressVos;
    }

    private String getRegionName(int id) {
        return regionMapper.selectByPrimaryKey(id).getName();
    }

    @Override
    public void deleteAddressById(Integer id) {
        Address address = new Address();
        address.setDeleted(true);
        address.setId(id);
        addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public Address getAddressDetailById(Integer id) {
        Address address = addressMapper.selectByPrimaryKey(id);
        String province=getRegionName(address.getProvinceId());
        address.setProvinceName(province);
        String city=getRegionName(address.getCityId());
        address.setCityName(city);
        String area=getRegionName(address.getAreaId());
        address.setAreaName(area);
        return address;
    }

    @Override
    public void updateAddress(Address address) {
        address.setUpdateTime(new Date());
        User user = GetNowUser.getUser();
        if (address.getIsDefault()) {
            //查找是否存在默认值,存在就把默认值改了
            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria();
            criteria.andIsDefaultEqualTo(true);
            Address address1 = new Address();
            address1.setIsDefault(false);
            //传入两个值,第一个为要修改的值,第二个为查找到的对象
            addressMapper.updateByExampleSelective(address1,addressExample);
        }
        if (address.getId() == 0) {
            address.setId(null);
            address.setUserId(user.getId());
            address.setAddTime(new Date());
            addressMapper.insertSelective(address);
            return;
        }
        addressMapper.updateByPrimaryKeySelective(address);
    }
}
